// pages/index/parkingdetail.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    if (options.id == -1){
      that.setData({
        memo: '',
        price: '',
        latitude: '',
        longitude: '',
        address: '',
        name: '',
        seqId:''
      })
    }else{
      
      wx.request({
        url: getApp().data.servser + '/parking/detail',
        header: {
          'content-type': 'application/x-www-form-urlencoded',
          'Authorization': 'Bearer ' + wx.getStorageSync('token')
        },
        data: {
          openId: app.globalData.openid,
          seqId: options.id
        },
        success: function (res) {
          that.setData({
            parkingInfo: res.data.data.parkingInfo,
            memo: res.data.data.parkingInfo.memo,
            price: res.data.data.parkingInfo.price,
            latitude: res.data.data.parkingInfo.latitude,
            longitude: res.data.data.parkingInfo.longitude,
            address: res.data.data.parkingInfo.address,
            name: res.data.data.parkingInfo.name,
            seqId: res.data.data.parkingInfo.seqId
          })
        }
      })
    }

  },
  getInputName: function(e) {
    this.data.name = e.detail.value;
  },
  getInputAddress: function(e) {
    this.data.address = e.detail.value;
  },
  getInputLongitude: function(e) {
    this.data.longitude = e.detail.value;
  },
  getInputLatitude: function(e) {
    this.data.latitude = e.detail.value;
  },
  getInputPrice: function(e) {
    this.data.price = e.detail.value;
  },
  getInputMemo: function(e) {
    this.data.memo = e.detail.value;
  },
  delete: function () {
    let that = this;
    wx.showModal({
      title: '提示',
      content: '确认删除吗？',
      success(res) {
        if (res.confirm) {
          wx.request({
            method: "POST",
            url: getApp().data.servser + '/parking/delete',
            header: {
              'content-type': 'application/x-www-form-urlencoded',
              'Authorization': 'Bearer ' + wx.getStorageSync('token')
            },
            data: {
              seqId: that.data.seqId
            },
            success: function (res) {
              wx.showToast({
                title: "删除成功",
                icon: 'none',
                duration: 2000
              })
              wx.navigateTo({
                url: 'register',
              })
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  add: function() {
    let that = this;
    wx.request({
      method: "POST",
      url: getApp().data.servser + '/parking/addItem',
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Authorization': 'Bearer ' + wx.getStorageSync('token')
      },
      data: {
        memo: that.data.memo,
        price: that.data.price,
        latitude: that.data.latitude,
        longitude: that.data.longitude,
        address: that.data.address,
        name: that.data.name
      },
      success: function (res) {
        wx.showToast({
          title: "新增成功",
          icon: 'none',
          duration: 2000
        })
        wx.navigateTo({
          url: 'register',
        })
      }
    })
  },
  update: function() {
    let that = this;

    if(that.data.seqId == ''){
      wx.request({
        method: "POST",
        url: getApp().data.servser + '/parking/addItem',
        header: {
          'content-type': 'application/x-www-form-urlencoded',
          'Authorization': 'Bearer ' + wx.getStorageSync('token')
        },
        data: {
          memo: that.data.memo,
          price: that.data.price,
          latitude: that.data.latitude,
          longitude: that.data.longitude,
          address: that.data.address,
          name: that.data.name
        },
        success: function (res) {
          wx.showToast({
            title: "新增成功",
            icon: 'none',
            duration: 2000
          })
          wx.navigateTo({
            url: 'register',
          })
        }
      })
    }else{
      wx.request({
        method: "POST",
        url: getApp().data.servser + '/parking/editItem',
        header: {
          'content-type': 'application/x-www-form-urlencoded',
          'Authorization': 'Bearer ' + wx.getStorageSync('token')
        },
        data: {
          seqId: that.data.seqId,
          memo: that.data.memo,
          price: that.data.price,
          latitude: that.data.latitude,
          longitude: that.data.longitude,
          address: that.data.address,
          name: that.data.name
        },
        success: function (res) {
          wx.showToast({
            title: "修改成功",
            icon: 'none',
            duration: 2000
          })
          wx.navigateTo({
            url: 'register',
          })
        }
      })
    }

    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})